package com.zhonggong.network.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.zhonggong.network.dao.StudentManager;
import com.zhonggong.network.entity.Student;

public class StudentManagerImpl implements StudentManager {
	
	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	
	List<Student> studentList;
	
	@Override
	public void load(String data_file) {
		// TODO Auto-generated method stub
		this.studentList = new ArrayList<Student>();
		InputStream input = StudentManagerImpl.class.getResourceAsStream(data_file);
		XSSFWorkbook wb = null;
		
		try {
			wb = new XSSFWorkbook(input);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		XSSFSheet sheet = wb.getSheetAt(1);
		
		for (int i = 1; i < sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Student s = new Student();
			
			s.setId(Long.valueOf(1000+i));
			
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = row.getCell(j).getStringCellValue().trim();
					if (j == 0) 
						s.setCode(value);
					if (j == 1) 
						s.setName(value);
					if (j == 2) 
						s.setAdminClass(value);
				}
				
			}
			logger.info("student_"+i+"is:"+s);
			this.studentList.add(s);
		}
		logger.info("size of student list is :" + this.studentList.size());
	}
		
}
